import numpy as np
import os

npy_path = 'E:/Research/2020ContrastiveLearningForSceneLabel/Data/20210329ExperimentData/round2/uncertainty'
anchor_referenceLabel_path = 'E:/Research/2020ContrastiveLearningForSceneLabel/Data/20210329ExperimentData/round2/anchorReferenceLabel_method2_exp2.txt' # 默认没有reference label

f = open(anchor_referenceLabel_path,'r')
lines = f.readlines()
f.close()
rf_label_anchor = [int(i[0]) for i in lines]

alerting_idx = []
for i, label in enumerate(rf_label_anchor):
    if label == 3:
        alerting_idx.append(i)

npy_file_num = 6

sim_matrixes = []
anchor_idx = [i * 16 + 7 for i in range(194)]
for i in range(1,npy_file_num+1):
    npy_file = os.path.join(npy_path, str(i)+'.npy')
    feat = np.load(npy_file)
    anchor_feat = feat[anchor_idx]
    sim_matrix = np.matmul(anchor_feat, anchor_feat.transpose(1,0))
    sim_matrixes.append(sim_matrix)

sim_matrixes = np.array(sim_matrixes)
sim_matrix_avg = np.average(sim_matrixes, axis=0)
sim_matrix_var = np.var(sim_matrixes, axis=0)

var_avg = np.average(sim_matrix_var, axis=1)

res = sorted(enumerate(var_avg), key=lambda x: x[1], reverse=True)
idx = [i[0] for i in res]
sorted_var = [i[1] for i in res]
sorted_label = np.array(rf_label_anchor)[idx]


for i in range(194):
    print('{:03d}\t{:d}\t{:.3f}'.format(i,sorted_label[i], sorted_var[i]))



pass

